import Vue from 'vue'
import VueRouter from 'vue-router'
import home from '../views/home/index'
import login from "@/views/login";
import desktop from "@/components/desktop";
import user from "@/components/user";
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'login',
    component: login
  },
  {
    path: '/chat',
    name: 'home',
    component: home,
    redirect: "/desktop",
    children:[
      {
        path: "/desktop",
        name: "desktop",
        component:desktop
      },
      {
        path: "/user",
        name: "user",
        component:user
      },
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next)=>{
  let token = localStorage.getItem("token");
  if(to.path === '/'){
    if (token){
      return next("/chat")
    }else {
      return next()
    }
  }
  if (!token){
    return next("/")
  }
  next()
})
export default router
